package downcasting;

import upcasting.Animal;
import upcasting.Cat;
import upcasting.Dog;

public class TestAnimal {
    public static void main(String[] args) {
        Cat cat = new Cat("元宝",2);
        Dog dog = new Dog("小七",1);

        //向上转型
        Animal animal = cat;
        animal.eat();
        animal = dog;
        animal.eat();

        //向下转型
        if(animal instanceof Cat){
            cat = (Cat)animal;//强制类型转换
            cat.funcation();
        }

        if(animal instanceof Dog){
            dog = (Dog)animal;
            dog.bark();
        }
    }
}
